package com.zheng.russiaRec.view 
{
	import com.zheng.russiaRec.model.P_App;
	import com.zheng.russiaRec.model.vo.VO_App;
	import com.zheng.russiaRec.staticData.EventData;
	import com.zheng.russiaRec.view.components.UI_GameHelp;
	import com.zheng.russiaRec.view.components.UI_GameSetting;
	import com.zheng.russiaRec.view.components.UI_GameStart;
	import com.zheng.utils.DebugTrace;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * app mediator
	 * @author rrDog
	 */
	public class M_App extends Mediator implements IMediator
	{
		//游戏场景
		private var appRoot:Sprite;	
		//棋盘
		private var m_cb:M_Checkerboard;
		//app数据
		private var p_app:P_App;
		
		public static const NAME:String = "M_NAME_APP";
		/**
		 * viewComponent.appRoot	游戏场景
		 * viewComponent.scoreMc	积分榜
		 * viewComponent.gameOverMc	游戏结束
		 */
		public function M_App(viewComponent:Object = null) 
		{
			super(NAME, viewComponent);
			appRoot = viewComponent.appRoot as Sprite;
			p_app=facade.retrieveProxy(P_App.NAME) as P_App;
		}
		override public function listNotificationInterests():Array 
		{
			return [ApplicationFacade.START_OVER, EventData.M_START_GAME, EventData.C_DEL_LINE
			,EventData.M_GAME_PAUSE,EventData.M_GAME_RESUME,EventData.C_GAMEOVER];
		}
		override public function handleNotification(notification:INotification):void 
		{
			switch (notification.getName()) 
			{
				//game start================================
				case ApplicationFacade.START_OVER:
					DebugTrace.monsterDebug(this, "游戏开始");
					showGameStartMc();
					p_app.currGameStage = VO_App.STAGE_STARTGAME;
					break;
				case EventData.M_START_GAME:
					startGame();
					break;
				//游戏加分
				case EventData.C_DEL_LINE:
					setScore();
					break;
				case EventData.M_GAME_PAUSE:
					showPauseMc();
					break;
				case EventData.M_GAME_RESUME:
					removePauseMc();
					break;
				case EventData.C_GAMEOVER:
					showGameOver();
					break;
			}
		}
		
		/**
		 * 初始化场景
		 * 1.画棋盘
		 * 2.注册键盘事件
		 * 3.设置棋盘坐标
		 * 4.显示积分榜
		 * 5.设置帧事件
		 */
		private function startGame():void {
			DebugTrace.monsterDebug(this, "初始化场景");
			regGameingKey();
			m_cb=facade.retrieveMediator(M_Checkerboard.NAME) as M_Checkerboard;
			appRoot.addChild(m_cb.cbUI);
			setDisplayPosition(m_cb.cbUI, p_app.cbP);
			showScore();
		}
		private function regGameingKey():void 
		{
			var obj:Object = new Object();
			obj.appRoot = appRoot.stage;
			var gameingKey:M_GameingKeyboard = new M_GameingKeyboard(obj);
			facade.registerMediator(gameingKey);
		}
		private function removeGameingKey():void 
		{
			facade.removeMediator(M_GameingKeyboard.NAME);
		}
		//gameStartMc===========================================================
		private function showGameStartMc():void 
		{
			var gameStartMc:UI_GameStart = new UI_GameStart();
			gameStartMc.gameStartBtn.addEventListener(MouseEvent.CLICK, onGameStartClick);
			gameStartMc.settingBtn.addEventListener(MouseEvent.CLICK, onSettingClick);
			gameStartMc.helpBtn.addEventListener(MouseEvent.CLICK, onHelpClick);
			
			viewComponent.gameStartMc = gameStartMc;
			appRoot.addChild(gameStartMc);
		}
		
		private function onHelpClick(e:MouseEvent):void 
		{
			showHelp();
			removeGameStartMc();
			p_app.currGameStage = VO_App.STAGE_HELP;
		}
		private function onSettingClick(e:MouseEvent):void 
		{
			showSetting();
			removeGameStartMc();
			p_app.currGameStage = VO_App.STAGE_SETTING;
		}
		
		private function onGameStartClick(e:MouseEvent):void 
		{
			sendNotification(EventData.M_START_GAME);
			removeGameStartMc();
			p_app.currGameStage = VO_App.STAGE_GAMEING;
			sendNotification(EventData.M_INISTAGE_OVER);
		}
		private function removeGameStartMc():void {
			var gameStartMc:UI_GameStart = viewComponent.gameStartMc as UI_GameStart;
			gameStartMc.gameStartBtn.removeEventListener(MouseEvent.CLICK, onGameStartClick);
			gameStartMc.settingBtn.removeEventListener(MouseEvent.CLICK, onSettingClick);
			gameStartMc.helpBtn.removeEventListener(MouseEvent.CLICK, onHelpClick);
			
			appRoot.removeChild(gameStartMc);
			viewComponent.gameStartMc = null;
		}
		//=========================================================================
		//setting==================================================================
		private function showSetting():void 
		{
			var gameSettingMc:UI_GameSetting = new UI_GameSetting();
			gameSettingMc.okBtn.addEventListener(MouseEvent.CLICK, onSetOver);
			gameSettingMc.quitBtn.addEventListener(MouseEvent.CLICK, onSetQuit);
			
			viewComponent.gameSettingMc = gameSettingMc;
			appRoot.addChild(gameSettingMc);
		}
		
		private function onSetQuit(e:MouseEvent):void 
		{
			removeSetting();
			showGameStartMc();
		}
		private function onSetOver(e:MouseEvent):void 
		{
			removeSetting();
			showGameStartMc();
		}
		private function removeSetting():void {
			var gameSettingMc:UI_GameSetting = viewComponent.gameSettingMc as UI_GameSetting;
			gameSettingMc.okBtn.removeEventListener(MouseEvent.CLICK, onSetOver);
			gameSettingMc.quitBtn.removeEventListener(MouseEvent.CLICK, onSetQuit);
			
			viewComponent.gameSettingMc = null;
			appRoot.removeChild(gameSettingMc);
		}
		//===============================================================
		//game help======================================================
		private function showHelp():void
		{
			var gameHelp:UI_GameHelp = new UI_GameHelp();
			gameHelp.quitBtn.addEventListener(MouseEvent.CLICK, onHelpQuit);
			
			viewComponent.gameHelp = gameHelp;
			appRoot.addChild(gameHelp);
		}
		
		private function onHelpQuit(e:MouseEvent):void 
		{
			removeHelp();
			showGameStartMc();
		}
		
		private function removeHelp():void
		{
			var gameHelp:UI_GameHelp = viewComponent.gameHelp as UI_GameHelp;
			gameHelp.quitBtn.removeEventListener(MouseEvent.CLICK, onHelpQuit);
			
			viewComponent.gameHelp = null;
			appRoot.removeChild(gameHelp);
		}
		//===============================================================
		private function setScorePanel():void{
			appRoot.addChild(viewComponent.scoreMc);
		}
		//scoreMc
		private function showScore():void {
			viewComponent.scoreMc=new ScoreMc();
			var mc:MovieClip=viewComponent.scoreMc;
			appRoot.addChild(mc);
			setDisplayPosition(mc,p_app.scoreP);
			mc.scoreTxt.text="0";
			mc.customsTxt.text="1";
		}
		private function showPauseMc():void
		{
			viewComponent.pauseMc = new GamePause();
			appRoot.addChild(viewComponent.pauseMc);
			setDisplayPosition(viewComponent.pauseMc, p_app.pauseP);
		}
		private function removePauseMc():void 
		{
			appRoot.removeChild(viewComponent.pauseMc);
			viewComponent.pauseMc = null;
		}
		private function removeScore():void 
		{
			appRoot.removeChild(viewComponent.scoreMc);
			viewComponent.scoreMc = null;
		}
		//加分
		private function setScore():void {
			var mc:MovieClip = viewComponent.scoreMc;
			mc.scoreTxt.text = p_app.perExp;
		}
		//game over
		private function showGameOver():void 
		{
			viewComponent.gameOverMc=new GameOverMc();
			var mc:MovieClip = viewComponent.gameOverMc;
			appRoot.addChild(mc);
			setDisplayPosition(mc,p_app.gameOverP);
		}
		private function removeGameOver():void 
		{
			appRoot.removeChild(viewComponent.gameOverMc);
			viewComponent.gameOverMc = null;
		}
		private function setDisplayPosition(display:DisplayObject,p:Point):void{
			display.x=p.x;
			display.y=p.y;
		}
	}

}